#include "fig_player_shadow.h"

#include "ui/ui_sprite.h"
#include "util/common_macros.h"


FIGPlayerShadow::FIGPlayerShadow(float CraftHeight) : m_CraftHeight(CraftHeight),
													  m_LastPosY(1.0f)
{

}


FIGPlayerShadow::~FIGPlayerShadow()
{
	Clear();
}



void	FIGPlayerShadow::Render(
								const math::Vec2& Offset,
								float Scale,
								ui::UISprite* pSprite
								)
{
	if(!pSprite)
		return;

	for(int i = 0; i < (int) m_Shadows.size(); i++)
	{
		FIGPlayerShadow::CraftShadow* pShadow = m_Shadows[i];
		if(pShadow->m_Alpha > 0)
		{
			pSprite->m_Color = ColorfRGBA( 1.0f,1.0f,1.0f,pShadow->m_Alpha);
			pSprite->m_Pos = pShadow->m_Pos + Offset;
			pSprite->RenderRotated();
		}
	}

}


void	FIGPlayerShadow::Tick(float dt, const math::Vec2& Pos)
{
	if(m_LastPosY - Pos.y > m_CraftHeight)
	{
		CraftShadow* pShadow = new FIGPlayerShadow::CraftShadow(Pos);
		m_Shadows.push_back(pShadow);
		m_LastPosY = Pos.y;

	}

	for(int i = 0; i < (int)m_Shadows.size(); i++)
	{
		CraftShadow* pShadow = m_Shadows[i];
		pShadow->m_Alpha -= dt;
	}
}


void	FIGPlayerShadow::Clear()
{
	for(int i = 0; i < (int)m_Shadows.size(); i++)
	{
		CraftShadow* pShadow = m_Shadows[i];
		UTIL_SAFE_DELETE(pShadow);
	}
	m_Shadows.resize(0);
	m_LastPosY = 1.0f;
}
